#include "delay.h"
#include "sys.h"
#include "usart.h"	 
#include "can.h" 
#include "oled.h"
#include "Display.h"
#include "stm32f10x_i2c.h"
#include "stm32f10x_rcc.h"
#include "IIC.h"

#define I2C1_SLAVE_ADDRESS7   0x30
#define ClockSpeed            200000

void IIC_init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;	
  I2C_InitTypeDef  I2C_InitStructure;

/* Enable peripheral clocks --------------------------------------------------*/
  /* Enable I2C1 clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  /* Enable GPIOB clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
  /* Configure I2C1 pins: SCL and SDA ----------------------------------------*/
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;	
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	
  I2C_DeInit(I2C1);	
  I2C_Cmd(I2C1, ENABLE);

  /* Enable I2C1 event and buffer interrupt */
  I2C_ITConfig(I2C1, I2C_IT_EVT | I2C_IT_BUF, ENABLE);
  
  /* I2C1 configuration ------------------------------------------------------*/
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;                 //本机地址
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;                              //IIC使能应答
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;//地址位
  I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;                           //时钟
  I2C_Init(I2C1, &I2C_InitStructure);                                      //初始化IIC1
  I2C_Cmd(I2C1, ENABLE);
}


/************************************************************/
        //函数名：RV_8564_C2_write
		//参  数: I2CX->使用的IIC号
        //        RV_8564_Addr->器件地址
        //        Reg_Addr ->要写的寄存器地址
        //        value  -> 写的数据
        //功  能: 写RV_8564_C2
//返回值: 0:正确，其余，IIC错误
/************************************************************/
uint8_t AD52XX_write(I2C_TypeDef *I2CX,uint8_t RV_8564_Addr,uint8_t Reg_Addr,uint8_t value)
{
	uint32_t i;
	i = 0;
	while(I2C_GetFlagStatus(I2CX,I2C_FLAG_BUSY))    //检查总线是否繁忙
	{
		i++;
		if(i>50000)
		{
		  I2C_GenerateSTOP(I2CX,ENABLE); //产生停止条件时,硬件自动清除TXE和BTF位	
		  return 1;	
		}		
	}
	
	I2C_GenerateSTART(I2CX,ENABLE);                  //主机产生开始信号

	i = 0;
	while(!I2C_CheckEvent(I2CX,I2C_EVENT_MASTER_MODE_SELECT))//判断SB是否为1,SB=1起始条件已经发送 即判断EV5
	{
		i++;
		if(i>50000)
		{
		  I2C_GenerateSTOP(I2CX,ENABLE); //产生停止条件时,硬件自动清除TXE和BTF位	
		  return 2;	
		}
	}
	I2C_Send7bitAddress(I2CX,RV_8564_Addr,I2C_Direction_Transmitter);   //发送地址
	i = 0;
	while(!I2C_CheckEvent(I2CX,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
	{
		i++;
		if(i>50000)
		{
		  I2C_GenerateSTOP(I2CX,ENABLE); //产生停止条件时,硬件自动清除TXE和BTF位	
		  return 3;	
		}		
	}
	I2C_SendData(I2CX,Reg_Addr);   //发送寄存器地址
	i = 0;
	while(!I2C_CheckEvent(I2CX,I2C_EVENT_MASTER_BYTE_TRANSMITTED))
	{
		i++;
		if(i>50000)
		{
		  I2C_GenerateSTOP(I2CX,ENABLE); //产生停止条件时,硬件自动清除TXE和BTF位	
		  return 4;	
		}		
	}
	I2C_SendData(I2CX,value);      //发送数据
//	while(!I2C_CheckEvent(I2CX,I2C_EVENT_MASTER_BYTE_TRANSMITTED));//判断TXE和BTF是否为1,TXE=1,BTF=1请求设置停止位 即判断EV8_2
	I2C_GenerateSTOP(I2CX,ENABLE); //产生停止条件时,硬件自动清除TXE和BTF位
	I2C_AcknowledgeConfig(I2CX,DISABLE);
	
	return 0;
}









